﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Apollo;
using Apollo.Modules.Commissioning;
using System.Globalization;

namespace Apollo.Website
{
    public partial class Controls_Dashboard_EmployeePayroll : System.Web.UI.UserControl
    {
        public Employee Employee { get; set; }
        protected CommissionPayrollCurrent payroll;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(Apollo.Application.CurrentUser is UserCommon)
                this.Employee = (Apollo.Application.CurrentUser as UserCommon).Employee;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            try
            {

                if (this.Employee != null)
                {
                    payroll = Apollo.Application.CurrentSession
                            .QueryOver<CommissionPayrollCurrent>()
                            .Where(cp => cp.Employee.Id == this.Employee.Id)
                            .SingleOrDefault<CommissionPayrollCurrent>();
                }

                if (Apollo.Application.CurrentUser.Role.Name != Apollo.Role.ADMIN_ROLE) {
                    if ((int)payroll.CommissionCalculation.State < 2) {
                        LiteralJS.Text = @"
        <script type=""text/javascript"">
            $(document).ready(function () {
                $(""#employee-payroll-tabs"").tabs();
            });
        </script>
";
                        return;                    
                    }
                }

                PopulatePayrollSummary();

                PopulateEmployeeSummary();

                LiteralJS.Text = @"
        <script type=""text/javascript"">
            $(document).ready(function () {
                $(""#employee-payroll-tabs"").tabs();

                " + this.GetPieJavaScript() + @"
            });
        </script>
";
            }
            catch {
                LiteralJS.Text = @"
        <script type=""text/javascript"">
            $(document).ready(function () {
                $(""#employee-payroll-tabs"").tabs();
            });
        </script>
";
            }
        }

        protected void PopulateEmployeeSummary() {
            if (this.Employee != null)
            {
                if (this.Employee.CalculationMonth != null)
                    LabelCalculationMonth.Text = this.Employee.CalculationMonth.ToString();

                if (this.Employee.Role != null)
                    LabelRole.Text = this.Employee.Role.Name;

                if (this.Employee.Position != null)
                    LabelPosition.Text = this.Employee.Position.Name;

                LabelCode.Text = this.Employee.InternalCode;

                //LabelQuaranty.Text = this.Employee.QuarantyGrossValue.ToString("0", CultureInfo.InvariantCulture) + " RON";
            }
            else
            {
                LabelCalculationMonth.Text = "-";
                LabelRole.Text = "-";
                LabelPosition.Text = "-";
                //LabelQuaranty.Text = "-";
                LabelCode.Text = "-";
            }
        }

        protected void PopulatePayrollSummary()
        {
            if (this.Employee != null)
                LabelEmployee.Text = this.Employee.FullName + " [" + this.Employee.InternalCode + "]";
            else
            {
                LabelEmployee.Text = "Grafic";
                LabelPayroll.Text = "Fluturas";
            }

            if (payroll != null)
            {
                LabelPayroll.Text = "Fluturas " + payroll.CommissionCalculation.Name;
                
                string navigateUrlTemplate = @"/View.aspx?MenuId=34&f={groupOp:""AND"",groups:[],rules:[{field:""CommissionType.Id"",op:""eq"",data:""{0}""},{field:""Employee.Id"",op:""eq"",data:""" + this.Employee.Id.ToString() + @"""},{field:""CommissionCalculation.Id"",op:""eq"",data:""" + this.payroll.CommissionCalculation.Id.ToString() + @"""}]}";
                string navigate2UrlTemplate = @"/View.aspx?MenuId=34&f={groupOp:""AND"",groups:[],rules:[{field:""CommissionType.Id"",op:""in"",data:""{0}""},{field:""Employee.Id"",op:""eq"",data:""" + this.Employee.Id.ToString() + @"""},{field:""CommissionCalculation.Id"",op:""eq"",data:""" + this.payroll.CommissionCalculation.Id.ToString() + @"""}]}";

                HyperLinkCA.Text = payroll.CACH.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkCA.NavigateUrl = navigateUrlTemplate.Replace("{0}", "1");

                HyperLinkCS.Text = payroll.CS.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkCS.NavigateUrl = navigateUrlTemplate.Replace("{0}", "2");

                HyperLinkCR.Text = payroll.CR.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkCR.NavigateUrl = navigateUrlTemplate.Replace("{0}", "3");

                HyperLinkCACD.Text = payroll.CACM.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkCACD.NavigateUrl = navigateUrlTemplate.Replace("{0}", "17");

                HyperLinkBP.Text = (payroll.BPC + payroll.BPOD).ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkBP.NavigateUrl = navigate2UrlTemplate.Replace("{0}", "11,12");

                HyperLinkPR.Text = (payroll.BPROD + payroll.BWOD).ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkPR.NavigateUrl = navigate2UrlTemplate.Replace("{0}", "30,8");

                HyperLinkBT.Text = payroll.BTEAM.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkBT.NavigateUrl = navigateUrlTemplate.Replace("{0}", "9");

                HyperLinkBO.Text = payroll.BOFFICE.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkBO.NavigateUrl = navigateUrlTemplate.Replace("{0}", "10");

                HyperLinkBBD.Text = payroll.BBD.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkBBD.NavigateUrl = navigateUrlTemplate.Replace("{0}", "15");

                HyperLinkCATP.Text = payroll.CATP.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkCATP.NavigateUrl = navigateUrlTemplate.Replace("{0}", "28");

                HyperLinkCAT.Text = (payroll.CAT + payroll.CATOD).ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkCAT.NavigateUrl = navigate2UrlTemplate.Replace("{0}", "7,26,27");

                HyperLinkCATR.Text = payroll.CATR.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkCATR.NavigateUrl = navigateUrlTemplate.Replace("{0}", "22");

                HyperLinkCHR.Text = payroll.CHR.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkCHR.NavigateUrl = navigateUrlTemplate.Replace("{0}", "23");

                HyperLinkOV.Text = (payroll.BOR + payroll.BODR).ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkOV.NavigateUrl = navigate2UrlTemplate.Replace("{0}", "13,14");

                HyperLinkCAD.Text = payroll.CAD.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkCAD.NavigateUrl = navigateUrlTemplate.Replace("{0}", "16");

                HyperLinkBST.Text = payroll.BST.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkBST.NavigateUrl = navigateUrlTemplate.Replace("{0}", "18");


                HyperLinkQ.Text = payroll.G.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkQ.NavigateUrl = navigateUrlTemplate.Replace("{0}", "19");

                HyperLinkGCLBK.Text = payroll.GCR.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkGCLBK.NavigateUrl = navigateUrlTemplate.Replace("{0}", "20");

                HyperLinkCLBK.Text = payroll.CLBK.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkCLBK.NavigateUrl = navigateUrlTemplate.Replace("{0}", "24");

                HyperLinkACT.Text = payroll.ACT.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkACT.NavigateUrl = navigateUrlTemplate.Replace("{0}", "32");

                HyperLinkACC.Text = payroll.ACC.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkACC.NavigateUrl = navigateUrlTemplate.Replace("{0}", "34");


                HyperLinkDCBK.Text = payroll.DCBK.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkDCBK.NavigateUrl = navigateUrlTemplate.Replace("{0}", "43");

                HyperLinkDNCF.Text = payroll.DNCF.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkDNCF.NavigateUrl = navigateUrlTemplate.Replace("{0}", "42");


                //if (this.Employee.NCFGrossValue < 0)
                //    LabelNCF.Text = (this.Employee.NCFGrossValue - payroll.NCF).ToString("0.0000", CultureInfo.InvariantCulture);
                //else if (payroll.NCF == 0)
                //    LabelNCF.Text = "0.0000";
                //else
                //    LabelNCF.Text = payroll.NCF.ToString("-0.0000", CultureInfo.InvariantCulture);


                if (payroll.CommissionCalculation.State == CommissionCalculationState.Locked)
                {
                    LabelGFund.Text = Math.Round(this.Employee.QuarantyGrossValue + payroll.G, 0).ToString("0.0000", CultureInfo.InvariantCulture);
                    LabelNCFFundPrev.Text = (this.Employee.NCFGrossValue + payroll.NCF).ToString("0.0000", CultureInfo.InvariantCulture);
                    LabelNCF.Text = this.Employee.NCFGrossValue.ToString("0.0000", CultureInfo.InvariantCulture);
                    LabelFCG.Text = Math.Round(this.Employee.FCGGrossValue + payroll.GCC + payroll.GCR, 0).ToString("0.0000", CultureInfo.InvariantCulture);
                }
                else
                {
                    LabelGFund.Text = this.Employee.QuarantyGrossValue.ToString("0.0000", CultureInfo.InvariantCulture);
                    LabelNCFFundPrev.Text = this.Employee.NCFGrossValue.ToString("0.0000", CultureInfo.InvariantCulture);
                    LabelNCF.Text = (this.Employee.NCFGrossValue - payroll.NCF).ToString("0.0000", CultureInfo.InvariantCulture);
                    LabelFCG.Text = this.Employee.FCGGrossValue.ToString("0.0000", CultureInfo.InvariantCulture);
                                        
                }

                HyperLinkQ.Text = payroll.G.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkQ.NavigateUrl = navigateUrlTemplate.Replace("{0}", "19");


                HyperLinkGCC.Text = payroll.GCC.ToString("0.0000", CultureInfo.InvariantCulture);
                HyperLinkGCC.NavigateUrl = navigateUrlTemplate.Replace("{0}", "33");

                LabelCommission.Text = Math.Round(payroll.GrossValue, 0).ToString("0.0000", CultureInfo.InvariantCulture);
                LabelTOTAL.Text = Math.Round(payroll.GrossValue + payroll.G + payroll.GCC, 0).ToString("0.0000", CultureInfo.InvariantCulture);
                

                
            }
            else
            {
                LabelPayroll.Text = "Fluturas";

                HyperLinkCA.Text = "0.0000";
                HyperLinkCA.NavigateUrl = "";

                HyperLinkCS.Text = "0.0000";
                HyperLinkCS.NavigateUrl = "";

                HyperLinkCR.Text = "0.0000";
                HyperLinkCR.NavigateUrl = "";

                HyperLinkCACD.Text = "0.0000";
                HyperLinkCACD.NavigateUrl = "";

                HyperLinkBP.Text = "0.0000";
                HyperLinkBP.NavigateUrl = "";

                HyperLinkPR.Text = "0.0000";
                HyperLinkPR.NavigateUrl = "";

                HyperLinkBT.Text = "0.0000";
                HyperLinkBT.NavigateUrl = "";

                HyperLinkBO.Text = "0.0000";
                HyperLinkBO.NavigateUrl = "";

                HyperLinkCATP.Text = "0.0000";
                HyperLinkCATP.NavigateUrl = "";

                HyperLinkCAT.Text = "0.0000";
                HyperLinkCAT.NavigateUrl = "";

                HyperLinkBBD.Text = "0.0000";
                HyperLinkBBD.NavigateUrl = "";

                HyperLinkCAD.Text = "0.0000";
                HyperLinkCAD.NavigateUrl = "";

                HyperLinkCATR.Text = "0.0000";
                HyperLinkCATR.NavigateUrl = "";

                HyperLinkCHR.Text = "0.0000";
                HyperLinkCHR.NavigateUrl = "";

                HyperLinkBST.Text = "0.0000";
                HyperLinkBST.NavigateUrl = "";

                HyperLinkOV.Text = "0.0000";
                HyperLinkOV.NavigateUrl = "";

                HyperLinkQ.Text = "-0.0000";
                HyperLinkQ.NavigateUrl = "";

                HyperLinkGCLBK.Text = "-0.0000";
                HyperLinkGCLBK.NavigateUrl = "";

                HyperLinkCLBK.Text = "0.0000";
                HyperLinkCLBK.NavigateUrl = "";


                HyperLinkACT.Text = "0.0000";
                HyperLinkACT.NavigateUrl = "";
                
                HyperLinkACC.Text = "0.0000";
                HyperLinkACC.NavigateUrl = "";

                HyperLinkDCBK.Text = "0.0000";
                HyperLinkDCBK.NavigateUrl = "";

                HyperLinkDNCF.Text = "0.0000";
                HyperLinkDNCF.NavigateUrl = "";


                LabelNCF.Text = "0.0000";
                LabelNCFFundPrev.Text = "0.0000";
                LabelGFund.Text = "0.0000";

                LabelCommission.Text = "0.0000";
                LabelTOTAL.Text = "0.0000";
            }
        }

        protected string GetPieJavaScript()
        {
            if (this.Employee == null)
                return "";
            
            if (payroll != null)
            {
                string series = "";
                series += GetSerie("Comision achizitie si training", payroll.CAT + payroll.CATOD);
                series += GetSerie("Comision achizitie si training partial", payroll.CATP);
                series += GetSerie("Comision achizitie si training reglare", payroll.CATR);
                series += GetSerie("Comision achizitie", payroll.CACH);
                series += GetSerie("Comision colectare", payroll.CS);
                series += GetSerie("Comision revalorizare", payroll.CR);
                series += GetSerie("Comision suma fixa", payroll.CACM);
                series += GetSerie("Bonus persistenta", payroll.BPC + payroll.BPOD);
                series += GetSerie("Bonus productivitate", payroll.BPROD + payroll.BWOD);
                series += GetSerie("Bonus echipa", payroll.BTEAM);
                series += GetSerie("Bonus office", payroll.BOFFICE);
                series += GetSerie("Bonus achizitie dublu", payroll.CAD);
                series += GetSerie("Bonus dezvoltare", payroll.BBD);
                series += GetSerie("Comision recrutare", payroll.CHR);
                series += GetSerie("Override", payroll.BOR + payroll.BODR);
                series += GetSerie("Prima Start-up", payroll.BST);

                if (series.EndsWith(","))
                    series = series.Substring(0, series.Length - 1);

                return @"
                $.plot(
                    $(""#" + this.PayrollPieContainer.ClientID + @"""),
                    [ " + series + @" ],
                    {
                        series: { 
                            pie: {
                                show: true,
                                combine: {
                                    color: '#999',
                                    threshold: 0.08,
                                    label: 'Alte comisioane'
                                },
                                radius: 1,
                                label: {
                                    show: true,
                                    radius: 3/4,
                                    formatter: function (label, series) {
                                        //return '<span class=""pie_label_text"" style=""background-color: '+ series.color +'"">' + label + '<br/>' + Math.round(series.percent) + '%</span>';  
                                        return '<div class=""pie_label_text"">' + label + '<br/>' + Math.round(series.percent) + '%</div>';
                                    },
                                    background: { opacity: 0.5 }/*,
                                    threshold: 0.05*/
                                }
                            } 
                        },
                        legend: { show: false }
                    }
                );
";
            }
            else
                return "";
        }

        protected string GetSerie(string name, double value)
        {
            return "{ label: \"" + name + "\", data: " + value.ToString("0.0000", CultureInfo.InvariantCulture) + " },";
        }
    }
}